<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Cache\Driver\Memcached;
/**
 * 检测后台登陆状态控制器
 */
class IsLoginController extends Controller{
	//初始化析构方法
	public function _initialize(){
		if(!isset($_SESSION['aid']) && !isset($_SESSION['username'])){
			$this->error('未登录或登录出错！',U('Admin/Login/index'));
		}
	}



	public function newWechat($uid=null){
		if(!$uid) $uid = $this->getUid();
		if($uid == NULL)  return false;
		$userData = M('wxapi')->where("wxuid={$uid}")->find();
		$m = new \Memcache();
		$m ->addServer("127.0.0.1", 11211);

		// 开发者中心-配置项-服务器配置-EncodingAESKey(消息加解密密钥)
		$encodingAESKey = 'ZOZxsuICmY614EBn41YgkRYZdOiCLruwpNxzWjEduKT';
		$wechat = new \Gaoming13\WechatPhpSdk\Wechat(
			array(
				'appId' 			=> $userData['appid'],
				'token' 			=> 	$userData['wxtoken'],
				'encodingAESKey'    =>	$encodingAESKey //可选
			)
		);

		$api = new \Gaoming13\WechatPhpSdk\Api(
			array(
				'appId' 			=> $userData['appid'],
				'appSecret'			=> $userData['appsecret'],
				'get_access_token'  => function() use($m){
					//读取token
					return $m->get('wechat_token'.I("uid"));
				},
				'save_access_token' => function($token)  use($m){
					//缓存token
					$m->set('wechat_token'.I('uid'), $token, 0);
				}
			)
		);
		return array($wechat,$api);
	}

	public function getUid(){
		if(empty($_GET['uid']) || empty($_SESSION['uid'])){
			$uid = NULL;
		}else if(!empty($_GET['uid'])){
			$uid = $_GET['uid'];
		}else if(empty($_GET['uid']) && !empty($_SESSION['uid'])){
			$uid = $_SESSION['uid'];
		}else{
			$uid = NULL;
		}
		return $uid;
	}

	public function Test(){
		$newWechat = $this->newWechat();
		list($wechat,$api) = $newWechat;

		$res = $api->create_menu('
			{
				"button":[
					{
						"name":"主菜单1",
						"sub_button":[
							{
								"type":"view",
								"name":"微博客",
								"url":"http://www.qipajun.com/"
							},
									{
								"type":"view",
								"name":"微信游戏",
								"url":"http://www.dashenti.com/index.php/Wap"
							},
									{
								"type":"view",
								"name":"败笔",
								"url":"http://wx.68sjw.cn/game/"
							}
						]
				   },
					{
						"name":"主菜单2",
						"sub_button":[
							{
								"type":"click",
								"name":"点击推事件",
								"key":"click_event1"
							},
							{
								"type":"view",
								"name":"微博客",
								"url":"http://www.qipajun.com/"
							},
							{
								"type":"scancode_push",
								"name":"扫码推事件",
								"key":"scancode_push_event1"
							},
							{
								"type":"scancode_waitmsg",
								"name":"扫码带提示",
								"key":"scancode_waitmsg_event1"
							}
						]
				   },
				   {
						"name":"主菜单3",
						"sub_button":[
							{
								"type":"pic_sysphoto",
								"name":"系统拍照发图",
								"key":"pic_sysphoto_event1"
							},
							{
								"type":"pic_photo_or_album",
								"name":"拍照或者相册发图",
								"key":"pic_photo_or_album_event1"
							},
							{
								"type":"pic_weixin",
								"name":"微信相册发图",
								"key":"pic_weixin_event1"
							},
							{
								"type":"location_select",
								"name":"发送位置",
								"key":"location_select_event1"
							}
						]
				   }
				]
			}');
		var_dump($res);
	}
}
